package com.jasonjan.jiance.adapter;

import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;

import com.jasonjan.jiance.bean.HomeNews;
import com.jasonjan.jiance.global.ErrorAction;

import java.util.List;

/**
 * Created by jasonjan on 2018/3/13.
 */

public class DiffCallback extends DiffUtil.Callback{

    public static final int HOME_NEWS=1;

    private List oldList,newList;
    private int type;

    public DiffCallback(List oldList,List newList,int type){
        this.oldList=oldList;
        this.newList=newList;
        this.type=type;
    }

    public static void notifyDataSetChanged(List oldList, List newList, int type, RecyclerView.Adapter adapter) {
        DiffCallback diffCallback = new DiffCallback(oldList, newList, type);
        DiffUtil.DiffResult result = DiffUtil.calculateDiff(diffCallback, true);
        result.dispatchUpdatesTo(adapter);
    }

    @Override
    public int getOldListSize() {
        return oldList != null ? oldList.size() : 0;
    }

    @Override
    public int getNewListSize() {
        return newList != null ? newList.size() : 0;
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition,int newItemPosition){
        try{
            switch(type){
                case HOME_NEWS:
                    return ((HomeNews.ResultBean.DataBean)oldList.get(oldItemPosition)).getTitle().equals(
                            ((HomeNews.ResultBean.DataBean)newList.get(newItemPosition)).getTitle());
            }
        }catch(Exception e){
            ErrorAction.print(e);
        }
        return false;
    }

    public boolean areContentsTheSame(int oldItemPosition,int newItemPosition){
        try{
            switch(type){
                case HOME_NEWS:
                    return ((HomeNews.ResultBean.DataBean)oldList.get(oldItemPosition)).getLink().equals(
                            ((HomeNews.ResultBean.DataBean)newList.get(newItemPosition)).getLink());
            }
        }catch(Exception e){
            ErrorAction.print(e);
        }
        return false;
    }
}
